<?php
    /**
     * Created by PhpStorm.
     * User: mybook-lhp
     * Date: 18/7/4
     * Time: 下午3:27
     */

    namespace app\payment\api;


    use app\common\controller\API;
    use app\member\model\MemberModel;
    use app\payment\model\PaymentInfoModel;
    use app\payment\model\PaymentWithdrawModel;


    /**
     * 提现(接入类)
     * Class WithdrawOrder
     *
     * @package app\payment\api
     */
    class Withdraw extends API
    {

        /**
         * 设置提现信息
         * api POST /payment/Withdraw/set_withdraw_info
         *
         * @param int $phone         手机验证码
         * @param int $validate_code 手机验证码
         *
         * @param     string         wxpay_name 微信真实姓名
         * @param     string         wxpay_account 微信账号
         *
         * @param     string         aliplay_name 支付宝真实姓名
         * @param     string         aliplay_account  支付宝账号
         *
         * @param     string         bank_name 银行名称
         * @param     string         bank_card_number 银行卡号
         * @param     string         bank_ture_name 持卡人姓名
         */
        public function set_withdraw_info()
        {

            $data = $this->request->param();
            empty($data) ? $this->error('提交失败') : null;
            $MemberModel   = MemberModel::field('phone')->where(['id' => env('member_id')])->find();
            $data['phone'] = $MemberModel['phone'];
            $this->check_validate($data,'Withdraw.set_withdraw_info');
            $data['member_id'] = env('member_id');;
            PaymentInfoModel::update($data,['member_id' => env('member_id')]) ? $this->success('提交成功！') : $this->error('提交失败！');
        }

        /**
         * 获取提现信息
         * api POST /payment/Withdraw/get_withdraw_info
         *
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function get_withdraw_info()
        {

            $this->result(PaymentInfoModel::where(['member_id' => env('member_id')])->find());
        }

        /**
         * 提交提现
         * api POST /payment/Withdraw/addwithdraw
         *
         * @param int $money 提现金额
         * @param int $type  提现方式(1微信2支付宝3银行卡)
         *
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         */
        public function addwithdraw()
        {

            $data              = $this->request->param();
            $data['member_id'] = env('member_id');;
            $this->check_validate($data,'Withdraw.addwithdraw');
            PaymentWithdrawModel::addwithdraw($data) ? $this->success('提交成功！等待审核') : $this->error('提交错误！');
        }

        /**
         * 提现手续费配置
         *
         * api POST /payment/Withdraw/getconfig
         */
        public function getconfig()
        {

            //            $config           = module_config('payment');
            $tips['title']    = '提现规则：';
            $tips['tips']     = [
                '1：提现收取2%手续费的。',
                '2：提现发起后，到账时间T+1个工作日',
            ];
            $data['tips']     = $tips;
            $data['proc_fee'] = 0.02;
            $this->result($data);
        }

        /**
         * 提现列表
         * api POST /payment/Withdraw/withdraw_lists
         *
         * @param int $page 当前页数
         *
         * @throws \think\exception\DbException
         */
        public function withdraw_lists()
        {

            $page = $this->request->param('page',0);
            $data = PaymentWithdrawModel::where([
                'member_id' => env('member_id')
            ])->order('id desc')->paginate($page,false,[
                'query' => request()->param()
            ]);
            if ($data)
            {
                $data = $data->toArray();
                foreach ($data['data'] as &$datum)
                {
                    $datum['amount_money'] = $datum['amount_money'] / 100;
                    $datum['proc_fee']     = $datum['proc_fee'] / 100;
                    $datum['true_money']   = $datum['true_money'] / 100;

                }
                unset($datum);
            }
            else
            {
                $data = [];
            }
            $this->result($data);
        }

        /**
         * 撤销订单
         * api POST /payment/Withdraw/backout
         *
         * @param string order_sn 提现订单号
         */
        public function backout()
        {

            $data = $this->request->param();
            $this->check_validate($data,'Withdraw.backout');
            PaymentWithdrawModel::backout($data['order_sn']) ? $this->success('撤销成功!') : $this->error('撤销失败！');;

        }
    }